<template>
	<view class="transfer-container">
		<view class="header">
			<view class="search-box">
				<input 
					type="text" 
					v-model="searchKeyword" 
					placeholder="搜索转接ID或会话ID" 
					@input="handleSearch"
				/>
				<text class="search-icon">🔍</text>
			</view>
			<view class="filter-box">
				<view class="date-range">
					<picker mode="date" :value="startDate" @change="onStartDateChange">
						<view class="picker-item">
							<text>开始日期: {{ startDate || '选择开始日期' }}</text>
						</view>
					</picker>
					<text>至</text>
					<picker mode="date" :value="endDate" @change="onEndDateChange">
						<view class="picker-item">
							<text>结束日期: {{ endDate || '选择结束日期' }}</text>
						</view>
					</picker>
				</view>
				<view class="status-filter">
					<text>状态: </text>
					<picker :range="statusOptions" :value="statusIndex" @change="onStatusChange">
						<view class="picker-item">
							<text>{{ statusOptions[statusIndex] }}</text>
						</view>
					</picker>
				</view>
			</view>
			<button class="search-btn" @click="loadTransfers">查询</button>
			<button class="reset-btn" @click="resetFilters">重置</button>
		</view>
		
		<view class="transfer-list">
			<view class="list-header">
				<text class="column transfer-id">转接ID</text>
				<text class="column session-id">会话ID</text>
				<text class="column from-service">源客服</text>
				<text class="column to-service">目标客服</text>
				<text class="column status">状态</text>
				<text class="column create-time">创建时间</text>
				<text class="column complete-time">完成时间</text>
				<text class="column operation">操作</text>
			</view>
			
			<view class="list-body">
				<view v-if="loading" class="loading">
					<text>加载中...</text>
				</view>
				<view v-else-if="transfers.length === 0" class="no-data">
					<text>暂无转接记录</text>
				</view>
				<view 
					v-else
					v-for="(transfer, index) in transfers" 
					:key="transfer.id" 
					class="transfer-item"
					:class="{ 'odd-row': index % 2 === 1 }"
				>
					<text class="column transfer-id">{{ transfer.id }}</text>
					<text class="column session-id">{{ transfer.sessionId }}</text>
					<text class="column from-service">{{ transfer.fromServiceId }}</text>
					<text class="column to-service">{{ transfer.toServiceId }}</text>
					<text class="column status" :class="'status-' + transfer.status">
						{{ formatStatus(transfer.status) }}
					</text>
					<text class="column create-time">{{ formatTime(transfer.createTime) }}</text>
					<text class="column complete-time">{{ transfer.completeTime ? formatTime(transfer.completeTime) : '-' }}</text>
					<view class="column operation">
						<button class="detail-btn" @click="viewDetail(transfer)">详情</button>
					</view>
				</view>
			</view>
		</view>
		
		<view class="pagination">
			<button 
				:disabled="currentPage <= 1" 
				@click="prevPage"
			>
				上一页
			</button>
			<text>{{ currentPage }} / {{ totalPages }}</text>
			<button 
				:disabled="currentPage >= totalPages" 
				@click="nextPage"
			>
				下一页
			</button>
		</view>
		
		<!-- 转接详情弹窗 -->
		<view v-if="showDetailModal" class="modal-overlay" @click="closeDetailModal">
			<view class="modal-content" @click.stop>
				<view class="modal-header">
					<text class="modal-title">转接详情</text>
					<text class="modal-close" @click="closeDetailModal">×</text>
				</view>
				<view class="modal-body">
					<view v-if="detailLoading" class="loading">加载中...</view>
					<view v-else>
						<view class="detail-row">
							<text class="detail-label">转接ID:</text>
							<text class="detail-value">{{ currentTransfer.id }}</text>
						</view>
						<view class="detail-row">
							<text class="detail-label">会话ID:</text>
							<text class="detail-value">{{ currentTransfer.sessionId }}</text>
						</view>
						<view class="detail-row">
							<text class="detail-label">源客服:</text>
							<text class="detail-value">{{ currentTransfer.fromServiceId }}</text>
						</view>
						<view class="detail-row">
							<text class="detail-label">目标客服:</text>
							<text class="detail-value">{{ currentTransfer.toServiceId }}</text>
						</view>
						<view class="detail-row">
							<text class="detail-label">状态:</text>
							<text class="detail-value">{{ formatStatus(currentTransfer.status) }}</text>
						</view>
						<view class="detail-row">
							<text class="detail-label">创建时间:</text>
							<text class="detail-value">{{ formatTime(currentTransfer.createTime) }}</text>
						</view>
						<view class="detail-row">
							<text class="detail-label">完成时间:</text>
							<text class="detail-value">{{ currentTransfer.completeTime ? formatTime(currentTransfer.completeTime) : '-' }}</text>
						</view>
						<view class="detail-row">
							<text class="detail-label">转接原因:</text>
							<text class="detail-value">{{ currentTransfer.reason || '-' }}</text>
						</view>
						<view class="detail-row" v-if="currentTransfer.status === 'REJECTED'">
							<text class="detail-label">拒绝原因:</text>
							<text class="detail-value">{{ currentTransfer.rejectReason || '-' }}</text>
						</view>
					</view>
				</view>
			</view>
		</view>
		
		<!-- 会话转接模态框 -->
		<view class="modal" v-if="showTransferModal">
			<view class="modal-mask" @click="closeTransferModal"></view>
			<view class="modal-content">
				<view class="modal-header">
					<view class="modal-title">转接会话</view>
					<view class="modal-close" @click="closeTransferModal">×</view>
				</view>
				<view class="modal-body">
					<view class="form-item">
						<view class="form-label">会话ID</view>
						<view class="form-value">{{ currentTransfer.sessionId }}</view>
					</view>
					<view class="form-item">
						<view class="form-label">转出客服</view>
						<view class="form-value">{{ getServiceName(currentTransfer.fromServiceId) }}</view>
					</view>
					<view class="form-item">
						<view class="form-label">转入客服</view>
						<view class="service-selector">
							<view class="search-box">
								<input type="text" v-model="serviceSearchKeyword" placeholder="搜索客服..." class="search-input" />
							</view>
							<view class="service-list">
								<view 
									v-for="service in filteredServices" 
									:key="service.id" 
									class="service-item"
									:class="{ 'selected': currentTransfer.toServiceId === service.id }"
									@click="selectService(service.id)"
								>
									<view class="service-item-badge" :class="{ 'online': service.serviceStatus === 'ONLINE' }"></view>
									<view class="service-item-info">
										<text class="service-code">{{ service.serviceCode }}</text>
										<text class="service-type">{{ service.serviceType === 'HUMAN' ? '人工客服' : '机器人客服' }}</text>
									</view>
									<view class="service-item-stats">
										<text class="service-sessions">当前会话: {{ service.currentSessions }}/{{ service.maxConcurrent }}</text>
									</view>
								</view>
								
								<view class="empty-tip" v-if="filteredServices.length === 0">
									<text>没有符合条件的客服</text>
								</view>
							</view>
						</view>
					</view>
					<view class="form-item">
						<view class="form-label">转接原因</view>
						<textarea class="form-textarea" v-model="currentTransfer.reason" placeholder="请输入转接原因..."></textarea>
					</view>
				</view>
				<view class="modal-footer">
					<button class="btn btn-cancel" @click="closeTransferModal">取消</button>
					<button class="btn btn-primary" @click="submitTransfer" :disabled="!currentTransfer.toServiceId">确认转接</button>
				</view>
			</view>
		</view>
	</view>
</template>

<script>
	import { getTransferHistory, getTransferDetail, getTransferHistoryPage } from '@/api/sessionTransfer.js';
	
	export default {
		data() {
			return {
				transfers: [],
				loading: false,
				searchKeyword: '',
				startDate: '',
				endDate: '',
				statusOptions: ['全部', '待处理', '已接受', '已拒绝', '已取消'],
				statusIndex: 0,
				currentPage: 1,
				pageSize: 10,
				totalPages: 1,
				totalCount: 0,
				
				// 详情弹窗
				showDetailModal: false,
				currentTransfer: {},
				detailLoading: false,
				
				// 会话转接模态框
				showTransferModal: false,
				serviceSearchKeyword: '',
				filteredServices: []
			}
		},
		mounted() {
			this.loadTransfers();
		},
		methods: {
			// 加载转接列表
			async loadTransfers() {
				try {
					this.loading = true;
					
					// 构建查询参数
					const params = {
						current: this.currentPage,
						pageSize: this.pageSize,
						keyword: this.searchKeyword,
					};
					
					if (this.startDate) {
						params.startDate = this.startDate;
					}
					
					if (this.endDate) {
						params.endDate = this.endDate;
					}
					
					if (this.statusIndex > 0) {
						switch(this.statusIndex) {
							case 1:
								params.status = 'PENDING';
								break;
							case 2:
								params.status = 'ACCEPTED';
								break;
							case 3:
								params.status = 'REJECTED';
								break;
							case 4:
								params.status = 'CANCELED';
								break;
						}
					}
					
					const res = await getTransferHistoryPage(params);
					if (res.code === 200) {
						this.transfers = res.data.records || [];
						this.totalCount = res.data.total || 0;
						this.totalPages = res.data.pages || 1;
					} else {
						uni.showToast({
							title: res.message || '加载转接记录失败',
							icon: 'none'
						});
					}
				} catch (err) {
					console.error('加载转接记录失败', err);
					uni.showToast({
						title: '加载转接记录失败',
						icon: 'none'
					});
				} finally {
					this.loading = false;
				}
			},
			
			// 查看转接详情
			async viewDetail(transfer) {
				try {
					this.currentTransfer = transfer;
					this.showDetailModal = true;
					this.detailLoading = true;
					
					const res = await getTransferDetail(transfer.id);
					if (res.code === 200) {
						this.currentTransfer = res.data;
					} else {
						uni.showToast({
							title: res.message || '加载转接详情失败',
							icon: 'none'
						});
					}
				} catch (err) {
					console.error('加载转接详情失败', err);
					uni.showToast({
						title: '加载转接详情失败',
						icon: 'none'
					});
				} finally {
					this.detailLoading = false;
				}
			},
			
			// 关闭详情弹窗
			closeDetailModal() {
				this.showDetailModal = false;
				this.currentTransfer = {};
			},
			
			// 分页相关
			prevPage() {
				if (this.currentPage > 1) {
					this.currentPage--;
					this.loadTransfers();
				}
			},
			nextPage() {
				if (this.currentPage < this.totalPages) {
					this.currentPage++;
					this.loadTransfers();
				}
			},
			
			// 筛选相关
			handleSearch() {
				this.currentPage = 1;
			},
			onStartDateChange(e) {
				this.startDate = e.detail.value;
			},
			onEndDateChange(e) {
				this.endDate = e.detail.value;
			},
			onStatusChange(e) {
				this.statusIndex = parseInt(e.detail.value);
			},
			resetFilters() {
				this.searchKeyword = '';
				this.startDate = '';
				this.endDate = '';
				this.statusIndex = 0;
				this.currentPage = 1;
				this.loadTransfers();
			},
			
			// 格式化和计算相关
			formatStatus(status) {
				switch(status) {
					case 'PENDING': return '待处理';
					case 'ACCEPTED': return '已接受';
					case 'REJECTED': return '已拒绝';
					case 'CANCELED': return '已取消';
					default: return status;
				}
			},
			formatTime(time) {
				if (!time) return '-';
				return new Date(time).toLocaleString();
			},
			
			// 会话转接相关
			closeTransferModal() {
				this.showTransferModal = false;
				this.currentTransfer = {};
			},
			selectService(serviceId) {
				this.currentTransfer.toServiceId = serviceId;
			},
			async submitTransfer() {
				try {
					const res = await this.$api.transfer.process(this.currentTransfer.id, {
						action: 'accept'
					});
					if (res.code === 200) {
						uni.showToast({
							title: '转接成功',
							icon: 'success'
						});
						this.loadTransfers();
					} else {
						uni.showToast({
							title: res.message || '转接失败',
							icon: 'none'
						});
					}
				} catch (err) {
					uni.showToast({
						title: '转接失败',
						icon: 'none'
					});
				}
			},
			getServiceName(serviceId) {
				// 实现获取服务名称的逻辑
				return '服务名称';
			}
		}
	}
</script>

<style>
	.transfer-container {
		padding: 20px;
		height: 100%;
		display: flex;
		flex-direction: column;
	}
	
	.header {
		display: flex;
		flex-wrap: wrap;
		gap: 10px;
		margin-bottom: 20px;
		padding-bottom: 15px;
		border-bottom: 1px solid #e0e0e0;
	}
	
	.search-box {
		position: relative;
		width: 300px;
	}
	
	.search-box input {
		width: 100%;
		padding: 8px 12px;
		padding-right: 35px;
		border: 1px solid #ddd;
		border-radius: 4px;
	}
	
	.search-icon {
		position: absolute;
		right: 10px;
		top: 50%;
		transform: translateY(-50%);
	}
	
	.filter-box {
		display: flex;
		gap: 15px;
		align-items: center;
	}
	
	.date-range {
		display: flex;
		align-items: center;
		gap: 5px;
	}
	
	.picker-item {
		padding: 8px 12px;
		border: 1px solid #ddd;
		border-radius: 4px;
		background-color: #f9f9f9;
	}
	
	.status-filter {
		display: flex;
		align-items: center;
		gap: 5px;
	}
	
	.search-btn, .reset-btn {
		padding: 8px 15px;
	}
	
	.search-btn {
		background-color: #007AFF;
		color: white;
	}
	
	.reset-btn {
		background-color: #f2f2f2;
	}
	
	.transfer-list {
		flex: 1;
		overflow: auto;
		border: 1px solid #e0e0e0;
		border-radius: 4px;
	}
	
	.list-header {
		display: flex;
		padding: 12px 15px;
		background-color: #f2f2f2;
		font-weight: bold;
	}
	
	.list-body {
		min-height: 200px;
	}
	
	.transfer-item {
		display: flex;
		padding: 12px 15px;
		border-bottom: 1px solid #e0e0e0;
	}
	
	.odd-row {
		background-color: #f9f9f9;
	}
	
	.column {
		overflow: hidden;
		text-overflow: ellipsis;
		white-space: nowrap;
	}
	
	.transfer-id {
		width: 10%;
	}
	
	.session-id {
		width: 12%;
	}
	
	.from-service, .to-service {
		width: 12%;
	}
	
	.status {
		width: 8%;
	}
	
	.create-time, .complete-time {
		width: 15%;
	}
	
	.operation {
		width: 15%;
		display: flex;
		gap: 5px;
	}
	
	.detail-btn {
		padding: 4px 8px;
		font-size: 14px;
		background-color: #007AFF;
		color: white;
	}
	
	.status-PENDING {
		color: #FF9500;
	}
	
	.status-ACCEPTED {
		color: #34C759;
	}
	
	.status-REJECTED {
		color: #FF3B30;
	}
	
	.status-CANCELED {
		color: #8E8E93;
	}
	
	.loading, .no-data {
		padding: 40px;
		text-align: center;
		color: #8E8E93;
	}
	
	.pagination {
		display: flex;
		justify-content: center;
		align-items: center;
		gap: 15px;
		margin-top: 20px;
	}
	
	.modal-overlay {
		position: fixed;
		top: 0;
		left: 0;
		right: 0;
		bottom: 0;
		background-color: rgba(0, 0, 0, 0.5);
		display: flex;
		justify-content: center;
		align-items: center;
		z-index: 999;
	}
	
	.modal-content {
		width: 80%;
		max-width: 600px;
		max-height: 80vh;
		background-color: white;
		border-radius: 8px;
		display: flex;
		flex-direction: column;
	}
	
	.modal-header {
		display: flex;
		justify-content: space-between;
		padding: 15px 20px;
		border-bottom: 1px solid #e0e0e0;
	}
	
	.modal-title {
		font-size: 18px;
		font-weight: bold;
	}
	
	.modal-close {
		font-size: 24px;
		cursor: pointer;
	}
	
	.modal-body {
		padding: 20px;
		overflow-y: auto;
	}
	
	.detail-row {
		display: flex;
		margin-bottom: 10px;
	}
	
	.detail-label {
		width: 100px;
		font-weight: bold;
	}
	
	.modal {
		position: fixed;
		top: 0;
		left: 0;
		right: 0;
		bottom: 0;
		background-color: rgba(0, 0, 0, 0.5);
		display: flex;
		justify-content: center;
		align-items: center;
		z-index: 999;
	}
	
	.modal-mask {
		position: fixed;
		top: 0;
		left: 0;
		right: 0;
		bottom: 0;
	}
	
	.modal-content {
		width: 80%;
		max-width: 600px;
		max-height: 80vh;
		background-color: white;
		border-radius: 8px;
		display: flex;
		flex-direction: column;
	}
	
	.modal-header {
		display: flex;
		justify-content: space-between;
		padding: 15px 20px;
		border-bottom: 1px solid #e0e0e0;
	}
	
	.modal-title {
		font-size: 18px;
		font-weight: bold;
	}
	
	.modal-close {
		font-size: 24px;
		cursor: pointer;
	}
	
	.modal-body {
		padding: 20px;
		overflow-y: auto;
	}
	
	.form-item {
		margin-bottom: 15px;
	}
	
	.form-label {
		font-weight: bold;
	}
	
	.form-value {
		margin-top: 5px;
	}
	
	.service-selector {
		position: relative;
	}
	
	.service-list {
		position: absolute;
		top: 100%;
		left: 0;
		width: 100%;
		max-height: 200px;
		overflow-y: auto;
		border: 1px solid #e0e0e0;
		border-radius: 4px;
		background-color: white;
	}
	
	.service-item {
		padding: 8px 12px;
		border-bottom: 1px solid #e0e0e0;
		cursor: pointer;
	}
	
	.selected {
		background-color: #f9f9f9;
	}
	
	.service-item-badge {
		width: 12px;
		height: 12px;
		border-radius: 50%;
		display: inline-block;
		margin-right: 8px;
	}
	
	.online {
		background-color: #34C759;
	}
	
	.service-item-info {
		display: flex;
		align-items: center;
	}
	
	.service-code {
		margin-right: 8px;
	}
	
	.service-type {
		font-size: 12px;
		color: #8E8E93;
	}
	
	.service-item-stats {
		font-size: 12px;
		color: #8E8E93;
	}
	
	.empty-tip {
		padding: 8px;
		text-align: center;
		color: #8E8E93;
	}
	
	.modal-footer {
		display: flex;
		justify-content: flex-end;
		padding: 15px;
		border-top: 1px solid #e0e0e0;
	}
	
	.btn {
		padding: 8px 15px;
		margin-left: 10px;
	}
	
	.btn-cancel {
		background-color: #f2f2f2;
	}
	
	.btn-primary {
		background-color: #007AFF;
		color: white;
	}
</style> 